//
//  DISAordergoodViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/10.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISAordergoodViewController.h"
#import "DISAorderGoodLeftViewController.h"//左侧 视图
#import "DISAorderGoodRightViewController.h"//右侧 视图

#import "CSSearchViewController.h" //搜索
#import "CSnewCategoryViewController.h" //分类

static int const labelWith = 60;

@interface DISAordergoodViewController ()<UIScrollViewDelegate,UITextFieldDelegate>{
}
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, strong) UIButton *oldButton;
@property (nonatomic, strong) UILabel *seletLabel;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, assign) BOOL isbutton;
@property (nonatomic, strong) UIImageView *leftImage;
@property (nonatomic, strong) UIImageView *rightImage;
@property (nonatomic, strong) UITextField *mytestfd;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *titlebackView;

@end

@implementation DISAordergoodViewController

-(NSMutableArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.mytestfd.text = @"";
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isbutton = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setScrollViewTopandBottom];
    [self setupViewControllers];
    [self setTitlesButton];
    
    self.backView.hidden = NO;
    // Do any additional setup after loading the view.
}

-(void)returnaction{
    [self.navigationController popViewControllerAnimated:YES];
}

//初始化标题ScrollView
-(void)setupScrollView{
    NSInteger count = self.childViewControllers.count;
    //设置标题滚动条
    self.titleScrollView.contentSize = CGSizeMake(count * labelWith, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    //设置内容滚动条
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*count, 0);
    //开启分页
    self.contentScrollView.pagingEnabled = YES;
    //没有弹簧效果
    self.contentScrollView.bounces = NO;
    //隐藏水平滚动条
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //设置协议
    self.contentScrollView.delegate = self;
}

//设置页面scrollView
-(void)setScrollViewTopandBottom{
    
    UIScrollView *contentScroll = [[UIScrollView alloc]init];
    contentScroll.frame = CGRectMake(0, kNaviHeight, ScreenW, KHEIGHT-60-kNaviHeight);
    contentScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentScroll];
    self.contentScrollView = contentScroll;
    self.contentScrollView.userInteractionEnabled = YES;
    self.contentScrollView.scrollEnabled = YES;;
    self.contentScrollView.pagingEnabled = YES;
    //显示水平方向的滚动条(默认YES)
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //显示垂直方向的滚动条(默认YES)
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*2, ScreenH-60);
    
    self.contentScrollView.delegate = self;
}

#pragma mark -- UISCrollview代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //CGFloat offsetX = index * ScreenW;
    
    UIButton *but = (UIButton *)[self.view viewWithTag:100+index];
    [self.oldButton setTitleColor:[self colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    
    [but setTitleColor:[self colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    if (self.oldButton == but) {
        
    } else {
        if (index == 0) {
            [but setImage:imgname(@"seleshangpin") forState:(UIControlStateNormal)];
            [self.oldButton setImage:imgname(@"unseledingdan") forState:(UIControlStateNormal)];
            [self shownavleft];
        } else {
            [self.oldButton setImage:imgname(@"unseleshangpian") forState:(UIControlStateNormal)];
            [but setImage:imgname(@"seledingdan") forState:(UIControlStateNormal)];
            [self shownavright];
        }
    }
    self.oldButton = but;
}

-(void)setTitlesButton{
    NSArray *arr = @[@"产品",@"订单"];
    NSArray *imagearr = @[@"seleshangpin",@"unseledingdan"];
    
    CGFloat wwww = ScreenW/arr.count;
    CGFloat hhhhh = 60;
    CGFloat yyyy = KHEIGHT-60;
    
    UIImageView *mybg = [[UIImageView alloc]initWithFrame:CGRectMake(0, yyyy, KWIDTH, hhhhh)];
    mybg.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mybg];
    
    mybg.userInteractionEnabled = YES;
    
    for (int i = 0; i<arr.count; i++) {
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(wwww*i, 0, wwww, hhhhh);
        [mybg addSubview:myButton];
        myButton.tag = 100+i;
        [myButton setImage:imgname(imagearr[i]) forState:(UIControlStateNormal)];
        [myButton setTitle:arr[i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [myButton setTitleColor:[ self colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        myButton.titleLabel.font = FontSize(12);
        myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        [myButton setTitleEdgeInsets:UIEdgeInsetsMake(myButton.imageView.frame.size.height+10 ,-myButton.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
        [myButton setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -myButton.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
        
        if (i == 0) {
            self.oldButton = myButton;
            [self titleClick:myButton];
            [self.oldButton setTitleColor:[self colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        }
    }
}

-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, kNaviHeight-40, KWIDTH, 40)];
        
        UITextField *testfd = [[UITextField alloc]initWithFrame:CGRectMake(38, 5, KWIDTH-78, 30)];
        testfd.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [_backView addSubview:testfd];
        testfd.layer.masksToBounds = YES;
        testfd.layer.cornerRadius =2;
        testfd.placeholder = @"     搜索产品";
        testfd.returnKeyType = UIReturnKeySearch;
        testfd.delegate = self;
        testfd.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
        testfd.leftViewMode = UITextFieldViewModeAlways;
        testfd.layer.masksToBounds = YES;
        testfd.layer.cornerRadius = 15;
        self.mytestfd =testfd;
        
        UIButton *searce = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [searce setImage:imgname(@"saosuo") forState:(UIControlStateNormal)];
        searce.frame = CGRectMake(KWIDTH-85, 0, 22, 40);
        [_backView addSubview:searce];
        [searce addTarget:self action:@selector(serachAction:) forControlEvents:(UIControlEventTouchUpInside)];
        UIButton *catebut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [catebut setImage:[UIImage imageNamed:@"categorybtdfs.png"] forState:(UIControlStateNormal)];
        catebut.frame = CGRectMake(KWIDTH-32, 0, 22, 40);
        [_backView addSubview:catebut];
        [catebut addTarget:self action:@selector(categoreAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        UIButton  *leftbutton = [[UIButton alloc] initWithFrame:CGRectMake(10, 3, 30, 30)];
        leftbutton.titleLabel.font = [UIFont systemFontOfSize:14];
        [leftbutton setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        [leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
        [leftbutton setTitleColor:KTEXTCOLOR forState:UIControlStateNormal];
        [leftbutton addTarget:self action:@selector(leftClickactin) forControlEvents:UIControlEventTouchUpInside];
        [_backView addSubview:leftbutton];
        [self.view addSubview: _backView];
    }
    return _backView;
}

- (void)leftClickactin{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)serachAction:(UIButton *)but{
    CSSearchViewController *vc = [CSSearchViewController new];
    vc.search = self.mytestfd.text;
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [super textFieldShouldReturn:textField];
    CSSearchViewController *vc = [CSSearchViewController new];
    vc.search = textField.text;
    [self.navigationController pushViewController:vc animated:YES];
    return YES;
}

- (void)categoreAction:(UIButton *)but{
    CSnewCategoryViewController *seleVc = [[CSnewCategoryViewController alloc]init];
    seleVc.myblock = ^(NSString * _Nonnull str, NSString * _Nonnull idstr) {
        //CSnewMallViewController *con = [[CSnewMallViewController alloc]init];
        //con.idstr = idstr;
        //con.str = str;
        //[self.navigationController pushViewController:con animated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhseleproduce" object:nil userInfo:@{@"keyidstr" : idstr}];
    };
    [self.navigationController pushViewController:seleVc animated:YES];
}

- (void)shownavleft{
    //backView.backgroundColor = [UIColor redColor];
    self.backView.hidden = NO;
    self.titlebackView.hidden = YES;
}

-(UIView *)titlebackView{
    
    if (!_titlebackView) {
        _titlebackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, kNaviHeight)];
        UILabel *titl = [[UILabel alloc]initWithFrame:CGRectMake(0, kNaviHeight-37, KWIDTH, 30)];
        titl.font = FontSize(18);
        titl.text = @"订货单";
        titl.textAlignment = NSTextAlignmentCenter;
        [_titlebackView addSubview:titl];
        
        UIButton  *leftbutton = [[UIButton alloc] initWithFrame:CGRectMake(10, kNaviHeight-37, 30, 30)];
        leftbutton.titleLabel.font = [UIFont systemFontOfSize:14];
        [leftbutton setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        [leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
        [leftbutton setTitleColor:KTEXTCOLOR forState:UIControlStateNormal];
        [leftbutton addTarget:self action:@selector(leftClickactin) forControlEvents:UIControlEventTouchUpInside];
        [_titlebackView addSubview:leftbutton];
        [self.view addSubview:_titlebackView];
        _titlebackView.hidden = YES;
    }
    return _titlebackView;
}

-(void)shownavright{
    self.titlebackView.hidden = NO;
    self.backView.hidden = YES;
}

-(void)titleClick:(UIButton *)but{
    //0取出label
    NSInteger index = but.tag-100;
    //2.1计算滚动位置
    CGFloat offsetX = index * ScreenW;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

//显示页面
-(void)showVC:(NSInteger)index {
    CGFloat offsetX = index * ScreenW;
    UIViewController *vc = self.childViewControllers[index];
    //判断当前控制器的View 有没有加载过 如果已经加载过 就不需要加载
    if (vc.isViewLoaded) return;
    vc.view.frame = CGRectMake(offsetX, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:vc.view];
}

- (void)setupViewControllers {
    DISAorderGoodLeftViewController *seveVC = [[DISAorderGoodLeftViewController alloc]init];
    seveVC.view.frame = CGRectMake(0, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:seveVC.view];
    [self addChildViewController:seveVC];
    
    DISAorderGoodRightViewController *evaluate = [[DISAorderGoodRightViewController alloc]init];
    evaluate.view.frame = CGRectMake(ScreenW, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:evaluate.view];
    [self addChildViewController:evaluate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
